// pages/apply.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ok:0,
    banner: "",
    cateList: [],
    show_warning: false,
    isSend: false,
    time: 60,
    btnSendText: '获取验证码', //倒计时格式：(60秒)
    opacity: 0,
    scrollTop: 0.5,
    top: 32,
    page: 'loading',
    sf_list: [],
    sf_tip: '请选择',
    city_list: [],
    city_tip: '请选择',
    id_big: '',
    upIng: {
      sfz_a: false,
      sfz_b: false,
      yszz: false
    },
    tel: '',
    ids: [],
    sub: {
      cate: [],
      name: '',
      tel: '',
      idcard: '',
      sfz_a: '',
      sfz_b: '',
      yszz: '',
      note: '',
      address: {
        title: ''
      }
    }
  },
  onPageScroll(e) {
    this.setData({
      scrollTop: e.scrollTop
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let obj = {};
    obj = wx.getMenuButtonBoundingClientRect();
    wx.getSystemInfo({
      success: (res) => {
        let top = obj.top ? (obj.top + (obj.height - 32) / 2) : (res.statusBarHeight + 32);
        let scrollH = res.windowWidth * 0.6;
        this.setData({
          top: top,
          scrollH: scrollH
        })
      }
    })
    this.load();
  },

  argee(){
    this.setData({ok:this.data.ok==1?0:1});
  },

  //选择地址、修改编辑地址
  chooseAddressView() {
    wx.chooseLocation({
      success: res => {
        if (!res.name || !res.address || !res.latitude || !res.longitude) {
          wx.util.toast('您没有选择地址');
          return;
        }
        this.setData({
          'sub.address': {
            title: res.name,
            address: res.address,
            latitude: res.latitude,
            longitude: res.longitude
          }
        })
      },
      fail: res => {
        wx.util.toast('获取地址失败');
      }
    });
  },
  setCate(e) {
    let index = e.currentTarget.dataset.index;
    let active = this.data.cateList[index];
    if (!active.hasOwnProperty('active')) {
      active.active = false;
    }
    this.setData({
      ['cateList[' + index + '].active']: !active.active
    })
  },
  setPickerVal(e) {
    let n = e.currentTarget.dataset.val;
    this.setData({
      [n + '_tip']: this.data[n + '_list'][e.detail.value].cityname
    })
  },
  setInput(e) {
    let key = e.currentTarget.dataset.key;
    this.setData({
      [key]: e.detail.value
    })
  },
  initNavigation(e) {
    this.setData({
      opacity: e.opacity,
      top: e.top
    })
  },
  opacityChange(e) {
    this.setData({
      opacity: e.opacity
    })
  },
  back() {
    this.goHome();
  },
  async load() {
    if (!wx.util.getToken()) {
      setTimeout(this.load, 200);
      return;
    }
    let res = await wx.util.post("apply/index");
    this.setData({
      page: res.data.page,
      cateList: res.data.list,
      city_list: res.data.city,
      'sub.tel': res.data.tel
    })
    if (this.data.page == "apply") {
      this.setData({
        banner: res.data.banner
      })
    }
    let ids = await wx.util.post("msg/get_wx_ids", {
      type: 'apply'
    });
    this.setData({
      ids: ids.data
    })
  },
  upload(e) {
    let type = e.currentTarget.dataset.type;
    let that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      success: function (res) {
        if (res.tempFilePaths.length == 0) {
          return false;
        }
        that.upAc(res.tempFilePaths[0], type);
      }
    });
  },
  async upAc(url, type) {
    this.setData({
      ['upIng.' + type]: true
    })
    let upload = await wx.util.uploadFile("upload/index", url);
    this.setData({
      ['upIng.' + type]: false,
      ['sub.' + type]: upload.url
    })
  },

  protocol(e){
    console.log('e',e)
    wx.navigateTo({
      // url:'/pages/articleDetail/articleDetail?id='+this.data.info.article1
      url:'/pages/articleDetail/articleDetail?id='+e.currentTarget.dataset.id
    })
  },

  async subAc() {
    if(this.data.ok == 0){
      wx.util.toast('请勾选我同意服务协议');
      return 0;
    }
    let cate = [];
    this.data.cateList.forEach(item => {
      if (item.active) {
        cate.push(item.id);
      }
    })
    this.setData({
      'sub.cate': cate,
      'sub.sf_tip': this.data.sf_tip,
      'sub.city_tip': this.data.city_tip
    })
    if (this.data.sub.cate.length == 0) {
      wx.util.toast('请选择您的技能');
      return;
    }
    console.log("可以提交了",this.data.sub)
    try {
      await wx.util.post("apply/add", this.data.sub, false);
      this.setData({
        page: 'wait'
      })
    } catch (e) {}

    if (this.data.ids.length > 0) {
      wx.requestSubscribeMessage({
        tmplIds: this.data.ids
      })
    }
  },
  goHome() {
    this.goCenter();
  },
  goCenter() {
    wx.switchTab({
      url: '/pages/my/my'
    })
  },
  formatIdNumber(e) {
    this.setData({
      id_big: e.detail.value
    })
  },
})